<template>
  <div>
    <div class="inspection_evaluation">
      <div class="panel_header">
        <i class="iconfont">&#xe66d;</i>
        供应商考察评估结果
      </div>
      <div class="inspection_evaluation_list">
        <el-form :model="form">
          <el-row class="el_row_bottom">
            <el-col :span="4" class="col_label">供应商编码</el-col>
            <el-col :span="4" class="col_view">{{form.vendorCode}}</el-col>
            <el-col :span="4" class="col_label">ERP代码</el-col>
            <el-col :span="4" class="col_view">{{form.vendorErpCode}}</el-col>
            <el-col :span="4" class="col_label">供应商名称</el-col>
            <el-col :span="4" class="col_view">{{form.vendorName}}</el-col>
          </el-row>
        </el-form>
        <el-form :model="result">
          <el-row class="el_row_bottom">
            <el-col :span="4" class="col_label">评分岗位</el-col>
            <el-col :span="4" class="col_view">{{result.sysStationName}}</el-col>
            <el-col :span="4" class="col_label">评估维度</el-col>
            <el-col :span="12 " class="col_view">{{result.assessDictItemName}}</el-col>
          </el-row>
        </el-form>        
        <el-form :model="factoryResult">
          <el-row class="el_row_bottom">
            <el-col :span="4" class="col_label">采购组织</el-col>
            <el-col :span="4" class="col_view">{{factoryResult.purOrgName}}</el-col>
            <el-col :span="4" class="col_label">采购组</el-col>
            <el-col :span="4" class="col_view">{{factoryResult.purGroupName}}</el-col>
            <el-col :span="4" class="col_label">品类</el-col>
            <el-col :span="4" class="col_view">{{factoryResult.categoryName}}</el-col>
          </el-row>
          <el-row class="el_row_bottom">
            <el-col :span="4" class="col_label">发布状态</el-col>
            <el-col :span="4" class="col_view">{{factoryResult.publishStatus == 1 ? '已发布' : '未发布'}}</el-col>
            <el-col :span="4" class="col_label">完成状态</el-col>
            <el-col :span="4" class="col_view">{{factoryResult.completeStatus == 1 ? '已完成' : '未完成'}}</el-col>
            <el-col :span="4" class="col_label">验证状态</el-col>
            <el-col :span="4" class="col_view">{{factoryResult.checkStatus== 1 ? '已验证' : '未验证'}}</el-col>
          </el-row>
        </el-form>
      </div>
      <div v-if="flag">
        <div class="panel-title">
          考察结论
        </div>
        <div class="inspection_evaluation_list">
          <el-form :model="result">
            <el-row class="el_row_bottom">
              <el-col :span="4" class="col_label">复评方式</el-col>
              <el-col :span="4" class="col_view">{{result.reviewWay == 1 ? '现场复审' : '整改报告'}}</el-col>
              <el-col :span="4" class="col_label"> 评分</el-col>
              <el-col :span="4" class="col_view">{{result.totalPoints}}</el-col>
              <el-col :span="4" class="col_label"> 结论</el-col>
              <el-col :span="4" class="col_view">{{result.resultFlag == 1 ? '合格' : '不合格'}}</el-col>
            </el-row>
            <el-row class="el_row_bottom">
              <el-col :span="4" class="col_label">是否需要整改</el-col>
              <el-col :span="4" class="col_view">{{result.improveFlag == 1 ? '是' : '否'}}</el-col>
              <el-col :span="4" class="col_label">评分附件</el-col>
              <el-col :span="12" class="col_view">
                <a href="javascript:;" @click="downloadFile(result.scoreFile.url,result.scoreFile.fileName)" class="download_color">{{result.scoreFile.fileName}}</a>
              </el-col>
            </el-row>
            <el-row class="el_row_bottom">
              <el-col :span="4" class="col_label">
                <div class="improveRequirement improveRequirement_left">整改意见</div>
              </el-col>
              <el-col :span="20" class="col_view">
                <div class="improveRequirement">{{result.improveRequirement}}</div>
              </el-col>
            </el-row>
            <div v-if="isShow">
              <div class="panel-title" style="width:100%">
                <i class="iconfont">&#xe66d;</i>
                整改反馈
              </div>
              <el-row class="el_row_bottom">
                <el-col :span="4" class="col_label">反馈附件</el-col>
                <el-col :span="20" class="col_view">
                  <a href="javascript:;" @click="download(result.attFileUrl,result.attFileName)" class="download_color">{{result.attFileName}}</a>
                </el-col>
              </el-row>
              <el-row class="el_row_bottom">
                <el-col :span="4" class="col_label">
                  <div class="improveRequirement improveRequirement_left">整改反馈</div>
                </el-col>
                <el-col :span="20" class="col_view">
                  <div class="improveRequirement">{{result.feedback}}</div>
                </el-col>
              </el-row>
            </div>
            <div v-if="isExamine">
              <div class="panel-title" style="width:100%">
                <i class="iconfont">&#xe66d;</i>
                审核反馈
              </div>
              <el-row class="el_row_bottom">
                <el-col :span="4" class="col_label">
                  <div class="improveRequirement improveRequirement_left">审核意见</div>
                </el-col>
                <el-col :span="20" class="col_view">
                  <div class="improveRequirement">{{result.checkOpinion}}</div>
                </el-col>
              </el-row>
            </div>
          </el-form>
        </div>
      </div>
      <div class="clearfix dialog-footer">
        <el-button class="fr" type="success" icon="el-icon-success" v-if="isScore" @click="dialogFormVisibler = true">评分</el-button>
        <el-button class="fr" type="success" icon="el-icon-success" v-if="isHide" @click="dialogFormVisible = true">审核</el-button>
      </div>
      <!-- 评分弹窗 -->
      <el-dialog title="评分" :visible.sync="dialogFormVisibler">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
          <el-form-item label="附件上传：">
            <el-upload class="upload-file" ref="upload" :action="this.lib.config.host.system + '/common/fileupload/upload'" :data=fileData
              :limit="1" :on-success="uploadSuccess" :file-list="fileList">
              <el-button slot="trigger" type="primary">选取文件</el-button>
            </el-upload>
          </el-form-item>
          <el-form-item label="评分" prop="totalPoints">
            <el-input v-model="ruleForm.totalPoints"></el-input>
          </el-form-item>
          <el-form-item label="结论" prop="resultFlag">
            <el-radio-group v-model="ruleForm.resultFlag">
              <el-radio :label="1">合格</el-radio>
              <el-radio :label="-1">不合格</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="是否需要整改" prop="improveFlag">
            <el-radio-group v-model="ruleForm.improveFlag" @change="rectify">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="2">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="整改意见" v-if="isShower" prop="improveRequirement">
            <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 6}" v-model="ruleForm.improveRequirement"></el-input>
          </el-form-item>
          <el-form-item label="复评方式" v-if="isShower" prop="reviewWay">
            <el-radio-group v-model="ruleForm.reviewWay">
              <el-radio :label="1">现场复审</el-radio>
              <el-radio :label="2">整改报告</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-time" @click="subForm('ruleForm','/vendormgt/dlv/factoryInspect/score/save')">暂存</el-button>
            <el-button type="success" icon="el-icon-success" @click="subForm('ruleForm','/vendormgt/dlv/factoryInspect/score/saveAndPublish')">发布</el-button>
            <el-button class="el_button_left" type="info" @click="dialogFormVisibler = false" icon="el-icon-d-arrow-left">返回</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>      
      <!-- 审核弹框 -->
      <el-dialog title="审核" :visible.sync="dialogFormVisible">
        <el-form :model="formInline" :rules="rules" ref="formInline" label-width="120px">
          <el-form-item label="审核意见" prop="checkOpinion">
            <el-input type="textarea" maxlength='200' :autosize="{ minRows: 2, maxRows: 6}" v-model="formInline.checkOpinion"></el-input>
          </el-form-item>
          <div class="clearfix dialog-footer">
            <el-button class="fr" @click="dialogFormVisible = false">取 消</el-button>
             <el-button class="fr fr_right" type="primary" @click="submitForm('formInline',-1)">驳回</el-button>
            <el-button class="fr fr_right" type="success" @click="submitForm('formInline',1)">通过</el-button>           
          </div>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>
<script>
  export default {
    data() {
      //正则验证评分
      var totalPoints = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('评分不能为空'));
        }
        var reg = /^(?:0|[1-9][0-9]?|100)$/;
        setTimeout(() => {
          if (!reg.test(value)) {
            callback(new Error('请输入0到100之间的评分!'));
          } else {
            callback()
          }
        }, 1000)
      };      
      return {
        fileList: [],
        // 定义附件的业务模块和业务动作
        fileData: {
          busAction: "VD_DLV_RESULT",
          tableName: "VD_DLV_ASSESS_RESULT",
          tableId: this.$route.query.id
        },     
        rules: {
          checkOpinion: [{
            required: true,
            message: '请填写审核意见',
            trigger: 'blur'
          }]
        },
        dialogFormVisible: false,
        dialogFormVisibler: false,
        isShow: false,
        isHide: false,
        isScore: false,
        isShower:false,
        isExamine: false,
        flag: false,
        form: {},
        result: {
          
        },
        factoryResult:{},
        formInline: {
          id: '',
          checkStatus: '',
          checkOpinion: ''
        },
        resultForm: {},
        rules: {

          totalPoints: [{
            validator: totalPoints,
            trigger: 'blur'
          }],
          resultFlag: [{
            required: true,
            message: '请选择结论',
            trigger: 'change'
          }],
          improveFlag: [{
            required: true,
            message: '请选择是否需要整改',
            trigger: 'change'
          }],
          improveRequirement:[
            { required: false, message: '请输入整改意见', trigger: 'blur' },
            { min: 0, max: 200, message: '最多能输入200个字符', trigger: 'blur' }            
          ],
          reviewWay: [{
            required: false,
            message: '请选择复评方式',
            trigger: 'change'
          }],
        },
        ruleForm: {
          totalPoints: '',
          resultFlag: '',
          reviewWay: '',
          resultType: 2, //附件评分 
          improveRequirement: '',
          improveFlag: '',
          id: this.$route.query.id,
          scoreAttId: '',
          checkFileName: '',
          checkFileUrl: ''
        }
      }
    },
    created() {
      this.init()
    },
    methods: {
      // 下载附件
      download(filePath, fileName) {
        this.downloadFile(filePath, fileName);
      },
      init() {
        //页面加载进来是请求暂存的数据
        this.axios.get(this.lib.config.host.vendorDlv + '/vendormgt/dlv/factoryInspect/score/scoreItemEdit/' + this.$route
            .query.id)
          .then((response) => {
            this.ruleForm = response.data;
            if (this.ruleForm.improveFlag == 1) {
              this.isShower = true
            } else {
              this.isShower = false
            }            
            this.fileList.push({
              url: response.data.scoreFile.url,
              name: response.data.scoreFile.fileName
            });         
          })
          .catch(function (error) {
            console.log(error);
          });        
        //获取供应商评分结果
        this.axios.post(this.lib.config.host.vendorDlv + '/vendormgt/dlv/factoryInspect/score/toViewScoreItem/' + this.$route
            .query.id)
          .then((response) => {
            this.factoryResult=response.data.factoryInspect;
            // this.factoryResult=response.data
            this.form = response.data.inspectItem;
            this.result = response.data;
            if(this.result.publishStatus == this.lib.common.status.NO){
              this.isScore = true;
            }
            else if(this.result.totalPoints){
              this.isScore = false;
              this.flag = true;
            }
            if(this.result.feedback){
              this.isHide = true;
              this.isShow = true;
            }
           if(this.result.checkOpinion){
              this.isHide = false;
              this.isExamine = true;
            }
          })
          .catch(function (error) {
            console.log(error);
          });
      },
      //通过
      submitForm(formName, checkStatus) {
        this.formInline.id = this.$route.query.id;
        this.formInline.checkStatus = checkStatus;
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$confirm('是否执行此操作?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              this.axios.post(this.lib.config.host.vendorDlv + '/vendormgt/dlv/factoryInspect/score/check',
                  this.formInline)
                .then((response) => {
                  this.dialogFormVisible = false;
                  this.$message({
                    type: 'success',
                    message: '操作成功!'
                  });
                  this.$router.push('/Index/VendorDlv/VendorInspectScoreList');
                })
                .catch((error) => {
                  console.log(error);
                })
            }).catch(() => {
              this.$message({
                type: 'info',
                message: '已取消此操作'
              });
            });
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      // 自动上传附件后返回附件url和name
      uploadSuccess: function (response) {
        //附件上传成功回调
        this.ruleForm.scoreAttId = response.id;
      },
      //判断是否整改 整改就显示整个框
      rectify() {
        if (this.ruleForm.improveFlag == 1) {
          this.isShower = true
        } else {
          this.isShower = false
        }
      },
     //暂存 保存发布
      subForm(formName, url) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$confirm('是否执行此操作?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              this.axios.post(this.lib.config.host.vendorDlv + url, this.ruleForm)
                .then((response) => {
                  this.$message({
                    type: 'success',
                    message: '操作成功!'
                  });
                  this.$router.push('/Index/VendorDlv/VendorInspectScoreList');
                })
                .catch((error) => {
                  console.log(error);
                })
            }).catch(() => {
              this.$message({
                type: 'info',
                message: '已取消此操作'
              });
            });
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      }
    }
  }

</script>
